package co.yixiang.modules.shop.serviceimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import co.yixiang.common.enums.GoodsSalesModeEnum;
import co.yixiang.common.exception.BadRequestException;
import co.yixiang.modules.goods.entity.domain.YxStoreProduct;
import co.yixiang.modules.shop.service.SalesModelRender;
import co.yixiang.modules.system.entity.domain.Wholesale;
import co.yixiang.modules.system.entity.dto.WholesaleDto;
import co.yixiang.modules.system.service.AgentHierarchyService;
import co.yixiang.modules.system.service.WholesaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class WholesaleSaleModelRenderImpl implements SalesModelRender {
    /**
     * 批发商品
     */
    @Autowired
    private WholesaleService wholesaleService;

    @Autowired
    private AgentHierarchyService hierarchyService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void renderSingle(List<WholesaleDto> wholesaleList, YxStoreProduct storeProduct) {
        Assert.notEmpty(wholesaleList, "批发规则不能为空");
        this.checkWholesaleList(wholesaleList, storeProduct);
        List<Wholesale> collect = wholesaleList.stream().sorted(Comparator.comparing(Wholesale::getPrice)).collect(Collectors.toList());
        wholesaleService.removeByGoodsId(storeProduct.getId());
        wholesaleService.saveOrUpdateBatch(collect);
    }


    private void checkWholesaleList(List<WholesaleDto> wholesaleList, YxStoreProduct storeProduct) {
        if (CollUtil.isEmpty(wholesaleList)) {
            // 批发规则为空抛出异常
            //throw new ServiceException(ResultCode.MUST_HAVE_SALES_MODEL);
        }
        final List<Long> collect = hierarchyService.listByIds(wholesaleList.stream().map(i -> i.getAgentHierarchyId()).collect(Collectors.toList())).stream().map(i -> i.getId()).collect(Collectors.toList());
        for (WholesaleDto wholesaleDto : wholesaleList) {
            if (wholesaleDto.getPrice() == null || wholesaleDto.getPrice() <= 0 || wholesaleDto.getNum() == null || wholesaleDto.getNum() <= 0) {
                // 校验价格、数量参数合法，否则抛出异常
                // throw new ServiceException(ResultCode.HAVE_INVALID_SALES_MODEL);
                throw new BadRequestException("批发规则存在小于等于0的无效数据！");
            }
            if (ObjectUtil.isEmpty(wholesaleDto.getAgentHierarchyId()) || !collect.contains(wholesaleDto.getAgentHierarchyId())) {
                // 代理信息校验
                // 抛出异常
                throw new BadRequestException("代理等级信息为空或者不存在！");
            }
            if (ObjectUtil.isEmpty(wholesaleDto.getGoodsId())) {
                wholesaleDto.setGoodsId(storeProduct.getId());
            }

        }
    }

    @Override
    public String getSalesMode() {
        return GoodsSalesModeEnum.WHOLESALE.name();
    }
}

